from . import db


class User(db.Model):

    __tablename__ = "li_user"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(80), doc="用户名", unique=True)
    password = db.Column(db.String(256), doc="用户密码")
    role_id = db.Column(db.Integer, db.ForeignKey("li_role.id", ondelete="CASCADE"))

    super_user = db.Column(db.Boolean, default=False)
    deleted = db.Column(db.Boolean, default=False)

    data_collection = db.relationship(
        "DataCollection",
        backref="users",
        lazy="dynamic",
        passive_deletes=False,
        cascade="delete",
    )
    ruel_cost = db.relationship(
        "RuelCost",
        backref="users",
        lazy="dynamic",
        passive_deletes=False,
        cascade="delete",
    )

    road_toll = db.relationship(
        "RoadToll",
        backref="users",
        lazy="dynamic",
        passive_deletes=False,
        cascade="delete",
    )

    parking_rate = db.relationship(
        "ParkingRate",
        backref="users",
        lazy="dynamic",
        passive_deletes=False,
        cascade="delete",
    )


class Role(db.Model):
    __tablename__ = "li_role"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    role_name = db.Column(db.String(80), doc="角色名称")
    users = db.relationship(
        "User", backref="roles", lazy="dynamic", passive_deletes=False, cascade="delete"
    )
    path = db.relationship(
        "PermissionPath", backref="roles", lazy="dynamic", passive_deletes=False, cascade="delete"
    )


class PermissionPath(db.Model):
    __tablename__ = "li_permission_path"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    path = db.Column(db.String(80), doc="路径")
    path_name = db.Column(db.String(80), doc="路径名称")
    icon = db.Column(db.String(80), doc="icon图标")
    role = db.Column(db.Integer, db.ForeignKey("li_role.id", ondelete="CASCADE"))
    son = db.relationship(
        "PermissionPathSon",
        backref="son_paths",
        lazy="dynamic",
        passive_deletes=False,
        cascade="delete",
    )


class PermissionPathSon(db.Model):
    __tablename__ = "li_permission_path_son"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    path = db.Column(db.String(80), doc="路径")
    path_name = db.Column(db.String(80), doc="路径名称")
    icon = db.Column(db.String(80), doc="icon图标")
    parent = db.Column(db.Integer, db.ForeignKey("li_permission_path.id", ondelete="CASCADE"))
